#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>

#include "mail.h"
#include "email.h"
#include "xdebug.h"
#include "common.h"
#include "util.h"

int gcount; 
GHashTable  *gmemhash;


void parse(const char* filename)
{
 	EMAIL	*email;
 	MAIL	*mail;
    GSList  *slist;
    GSList  *addr = NULL;
    char    text[8192];
    
    INFO("parse mail: %s\n", filename);
 	mail = mail_new(filename, "/home/zhaowei/111");
    mail_parse(mail, 1);
    
    INFO("try print mailhead...\n");
    mailhead_print(mail->MailHead);
    
    addr = addr_parse(mail->MailHead->To);
    addrs_print(addr);
    addrs_delete_all(addr);
    
    text_decode(text, mail->MailHead->Subject, "GB2312");
    INFO("Subject: %s\n", text);

    slist = mail->MailMime;

    while (slist) {
        //INFO("slist: %p\n", slist);
        mime_print(slist->data);
        slist = g_slist_next(slist);
    }

    mail_delete(mail);

    e_print(gmemhash);
}


int scan_dir(char *aszDir)
{
    char    lszFileName[1024 + 1] = {0};
    DIR     *lpDir=NULL;
    struct  dirent *lpDirEntry=NULL;
    struct stat statbuf;

    if (NULL == aszDir) {
        ERROR("NULL == aszDir\n");
        return -1;
    }

    lpDir = opendir(aszDir);
    if (NULL == lpDir) {
        ERROR("error to opendir: %s\n", aszDir);
        return -2;
    }

    while((lpDirEntry = readdir(lpDir))) {
        if((strcmp(lpDirEntry->d_name, ".") == 0) ||
                (strcmp(lpDirEntry->d_name, "..") == 0))
            continue;

        strncpy(lszFileName, aszDir, 1024);

        if ('/' != lszFileName[strlen(lszFileName) - 1]) {
            strcat(lszFileName, "/");
        }

        strcat(lszFileName, lpDirEntry->d_name);
        lstat(lszFileName, &statbuf);

        if (S_ISREG(statbuf.st_mode)) {
            gcount++;
            INFO("%d check %s\n", gcount, lszFileName);
            fprintf(stderr, "%d check %s\n", gcount, lszFileName);
            parse(lszFileName);
        } else if (S_ISDIR(statbuf.st_mode)) {
            scan_dir(lszFileName);
        }
    }

    closedir(lpDir);

    return 0;
}


int main(int argc, char *argv[])
{
    int ret;

    gcount = 0;
    
    gmemhash = e_init(NULL);
    
    if (argc == 2) {
        if (isdir(argv[1])) {
            scan_dir(argv[1]);
        }else if (isfile(argv[1])) {
            parse(argv[1]);
        }else {
            printf("usage: keenness filename/dirname\n");
        }
    }else if (argc == 4){
        int     offset, len;
        char    *filename;
        char    *savefile = "/tmp/11111.txt";

        filename = argv[1];
        offset = atoi(argv[2]);
        len    = atoi(argv[3]);
        
        INFO("filename: %s, offset: %d, len: %d, save: %s\n", filename, offset,len, savefile);
        ret = file_range_decode(filename, offset, len, 'b', savefile);
        INFO("ret: %d\n", ret);

    }else{
        printf("usage: keenness filename/dirname\n");
    }
    
    e_destroy(gmemhash);


    return 0;
}

